package dao;
import java.sql.*;
 
public class Transaction {

	private Connection con = null;

	public Transaction() {
	}

	public Connection getConnection() throws TransactionException {
		//patrón SINGLETOM
		//if (con == null) {
			try {
                            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
                            Connection con = DriverManager.getConnection("jdbc:sqlserver://ALEX1:1433;" +
                                "databaseName=BDFarmacia;user=sa;password=dba;");
                            con.setAutoCommit(false);
                            return con;
			} catch (SQLException e) {
                            throw new TransactionException(e);
			}
		//}
		//return con;
	}

	public void commit() throws TransactionException {
		try {
			if (con != null)
				con.commit();
		} catch (SQLException e) {
			throw new TransactionException(e);
		}

	}

	public void rollback() {
		try {
			if (con != null)
				con.rollback();
		} catch (SQLException ignored) {
			ignored.printStackTrace();
		}
	}

	public void close() {
		try {
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (SQLException ignored) {
			ignored.printStackTrace();
		}
	}
}